var defalutParticipatorId = 0;
var defalutAwardsDateType = 0;
var onloading = false;
var defalutMsgSize = 4;
var layer;
layui.use(['layer'], function(){
    layer = layui.layer;
});

/***
 * 留言
 */
function addMsgFunc(){

    // 拼装参数
    var obj = new Object();
    obj.awardsParticipatorId = defalutParticipatorId;
    obj.message = $("#message").val();
    if (obj.message == undefined || obj.message == '') return;

    // 避免重复请求
    if (onloading) return;
    onloading = true;

    // 发起请求
    $.post("/portals/vote/jsp/voteAwardsMessage/add.jsp", obj, function(resp){
        if (resp.code == 200){
            $("#awardsParticipatorMessageNum").text($("#awardsParticipatorMessageNum").text()*1+1);
        } else if (resp.msg=='请重新登录'){
            layer.alert("请重新登录");
        }else {
            console.error(resp.msg)
        }
    });
    // 重新加载
    $("#message").val("");
    $.get("/portals/vote/jsp/voteAwardsMessage/list.jsp?awardsParticipatorId="+defalutParticipatorId, function(resp){
        if (resp.code == 200){
            var data = resp.data;
            // 总数
            $("#msgCount").text(data.length);
            // 循环
            var html = '';
            var index = 1;
            data.forEach(function(json){
                html +='<div class="df liuyan '+(index>defalutMsgSize?'hidden':'')+'">';
                html +=' <div class="liuyan-toxiang">';
                html +='  <img src="'+json.photo+'">';
                html +=' </div>';
                html +=' <div class="liuyan-name">';
                html +='   <div class="mt5">'+json.name+' <span class="fr c9e9e9e f12">'+json.timer+'</span></div>';
                html +='   <div class="c9e9e9e f2 mt10">'+json.dept+'</div>';
                html +='   <div class="c9e9e9e mt10">'+json.message+'</div>';
                html +='  </div>';
                html +='</div>';
                index++;
            });
            console.log(data.length+'-----'+defalutMsgSize+'----------'+(data.length<=defalutMsgSize))
            if(data.length<=defalutMsgSize){
                $("#msgMoreBtn").hide(100);
            }else {
                $("#msgMoreBtn").show(100);
            }
        }else {
            $("#msgCount").text(0);
            console.log(resp.msg)
        }
        $("#msgList").empty();
        $("#msgList").append(html);
    });
    onloading = false;
}

/***
 * 加载更多留言信息
 */
function msgMoreFunc(){
    $(".liuyan").removeClass("hidden");
    $("#msgMoreBtn").hide(200);
}

/*****
 * 当前用户对提名名单的投票和送花信息
 * @param participatorId
 */
function voteAndFlowerFunc(participatorId) {
    $.get("/portals/vote/jsp/front/getVoteAndFlower.jsp?awardsParticipatorId="+participatorId,function(resp){
        if(resp.code == 200){
            // 是否投过票
            if (resp.data.votesStatus){
                $("#tpBtn").text("已投票").unbind();
            }else {
                $("#tpBtn:parent").addClass("c5f9dab");
                $("#tpBtn").text("投票").one("click", function(){
                    layer.prompt({title: '确认投票', formType: 2}, function(pass, index) {
                        if (pass == undefined || pass == '') {
                            layer.msg('请输入投票留言信息');
                        } else {
                            $.post("/portals/vote/jsp/voteAwardsBallot/add.jsp?awardsParticipatorId=" + participatorId, function (resp2) {
                                if (resp2.code == 200 && resp2.msg != '活动已结束') {
                                    $("#awardsParticipatorVotesNum").text($("#awardsParticipatorVotesNum").text() * 1 + 1);
                                    $("#tpBtn").text("已投票").unbind();
                                    $("#tpBtn:parent").removeClass("c5f9dab");
                                    $("#message").val(pass)
                                    addMsgFunc();
                                    layer.close(index);
                                }  else if (resp2.code == 200) {
                                    layer.msg(resp2.msg);
                                    layer.close(index);
                                } else if (resp.msg == '请重新登录') {
                                    layer.msg("请重新登录");
                                    layer.close(index);
                                } else {
                                    console.warn(resp2.msg);
                                }
                            });
                        }
                    })
                });
            }
            // 是否送过花
            if (resp.data.flowerStatus){
                $("#shBtn").text("已送花").unbind();
            }else {
                $("#shBtn:parent").addClass("c5f9dab");
                $("#shBtn").text("送花").one("click", function(){
                    $.post("/portals/vote/jsp/voteAwardsFlower/add.jsp?awardsParticipatorId="+participatorId,function (resp3) {
                        if (resp3.code == 200 ){
                            $("#awardsParticipatorFlowerNum").text($("#awardsParticipatorFlowerNum").text()*1+1);
                            $("#shBtn").text("已送花").unbind();
                            $("#shBtn:parent").removeClass("c5f9dab");
                        } else if (resp.msg=='请重新登录') {
                            layer.msg("请重新登录");
                        } else {
                            console.warn(resp3.msg);
                        }
                    });
                });
            }
        }else {
            console.log(resp.msg)
        }
    });
}

$(function() {
    // 避免重复请求
    if (onloading) return;
    onloading = true;
    // 查询用户资料
    defalutParticipatorId = getQueryVariable("id");
    defalutAwardsDateType = getQueryVariable("type");
    $.ajaxSettings.async = false;
    $.get("/portals/vote/jsp/voteAwardsParticipator/info.jsp?id="+defalutParticipatorId, function(resp){
        if (resp.code == 200 ){
            var data = resp.data;
            $("#awardsParticipatorPhoto").attr("src",data.nominatorPhoto);
            if (data.nominatorType==1){ // 个人提名
                $("#awardsParticipatorName").text(data.memberNames);
                $("#awardsParticipatorDeptName").text(data.nominatorDeptName);
                $("#awardsParticipatorPosition").text(data.nominatorPosition);
                $("#awardsParticipatorType1").show();
                $("#awardsParticipatorType2").hide();
            } else {                    // 团队提名
                $("#awardsParticipatorPoject").text(data.pojectNum);
                $("#awardsParticipatorPojectMember").text(data.memberNames);
                $("#awardsParticipatorType1").hide();
                $("#awardsParticipatorType2").show();
            }
            $("#awardsParticipatorVotesNum").text(data.votesNum);
            $("#awardsParticipatorFlowerNum").text(data.flowerNum);
            $("#awardsParticipatorMessageNum").text(data.messageNum);
            if (defalutAwardsDateType==1){
                $("#isShowTP").show();
            }else {
                $("#isShowTP").hide();
            }
            $("#awardsParticipatorComplete").html(data.complete);
            $("#awardsParticipatorInfluence").html(data.influence);
            $("#awardsParticipatorPojectGoal").text(data.projectGoal);
            if (resp.data.length==0){
                $("#msgMoreBtn").hide(200);
            }
        } else {
            console.log(resp.msg)
        }
    });
    // 留言信息
    $("#message").val("");
    $.get("/portals/vote/jsp/voteAwardsMessage/list.jsp?awardsParticipatorId="+defalutParticipatorId, function(resp){
        if (resp.code == 200 ){
            // 总数
            $("#msgCount").text(resp.data.length);
            // 循环
            var html = '';
            var index = 1;
            resp.data.forEach(function(json){
                html +='<div class="df liuyan '+(index>defalutMsgSize?'hidden':'')+'">';
                html +=' <div class="liuyan-toxiang">';
                html +='  <img src="'+json.photo+'">';
                html +=' </div>';
                html +=' <div class="liuyan-name">';
                html +='   <div class="mt5">'+json.name+' <span class="fr c9e9e9e f12">'+json.timer+'</span></div>';
                html +='   <div class="c9e9e9e f12 mt10">'+json.dept+'</div>';
                html +='   <div class="c9e9e9e f12 mt10">'+json.message+'</div>';
                html +='  </div>';
                html +='</div>';
                index++;
            });
            $("#msgMoreBtn").removeClass("hidden");
            if(resp.data.length<=defalutMsgSize){
                $("#msgMoreBtn").addClass("hidden");
            }
            $("#msgList").empty();
            $("#msgList").append(html);
        } else {
            $("#msgCount").text(0);
            console.log(resp.msg)
        }
    });
    voteAndFlowerFunc(defalutParticipatorId);
    // 是否显示投票的窗口
    if (defalutAwardsDateType == 1){
        $("#isShowTPFunc").show();
    }else {
        $("#isShowTPFunc").hide();
    }
    onloading = false;
});